package com.right.handler;

import com.right.command.CsCommand;
import com.right.db.Bicycle;
import com.right.db.BicycleLock;
import com.right.db.BicycleTemporariness;
import com.right.factory.NodeStatus;
import com.right.mapper.BicycleLockMapper;
import com.right.mapper.BicycleMapper;
import com.right.mapper.BicycleTemporarinessMapper;
import com.right.util.Crc16;
import com.right.util.StringUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 *     @author : pillar
 *     e-mail : 347637454@qq.com
 *     time   : 2018/12/19 16:09
 *     desc   :
 *     version: 1.0
 * </pre>
 */
@Slf4j
public class LoginHandler implements PackageHandler {

    private final BicycleTemporarinessMapper bicycleTemporarinessMapper;
    private final BicycleMapper bicycleMapper;
    private final BicycleLockMapper bicycleLockMapper;

    public LoginHandler(BicycleTemporarinessMapper bicycleTemporarinessMapper,
                        BicycleMapper bicycleMapper,
                        BicycleLockMapper bicycleLockMapper) {
        this.bicycleTemporarinessMapper = bicycleTemporarinessMapper;
        this.bicycleMapper = bicycleMapper;
        this.bicycleLockMapper = bicycleLockMapper;
    }


    @Override
    public boolean applies(List<String> dataList) {
        return CsCommand.LOGIN.equals(dataList.get(1));
    }

    @Override
    public boolean handleMessage(ChannelHandlerContext ctx, List<String> dataList) {
        log.info("登录报文={}", dataList);

        String lockNo = dataList.get(0);

        if (StringUtil.isEmpty(lockNo)) {
            log.info("未上送设备号");
            return false;
        }

        BicycleTemporariness b = new BicycleTemporariness();
        b.setLockNo(lockNo);
        int count = bicycleTemporarinessMapper.selectCount(b);

        if (count == 1) {
            // 存在
            saveAndCheckLockType(lockNo, ctx);
        } else {
            // 不存在
            Bicycle r = new Bicycle();
            r.setLockNo(lockNo);
            count = bicycleMapper.selectCount(r);
            if (count == 1) {
                // 将锁编号存入 通道中
                saveAndCheckLockType(lockNo, ctx);
            } else {
                log.info("锁编号：" + lockNo + "未录入");
            }
        }

        return false;
    }

    private void saveAndCheckLockType(String lockNo, ChannelHandlerContext ctx) {
        // 将锁编号存入 通道中
        NodeStatus.getInstance().putClientStat(lockNo, ctx.channel());
        // 查询锁类型
        Example example = new Example(BicycleLock.class);

        example.createCriteria().andEqualTo("lockNo", lockNo);

        BicycleLock bicycleLock = bicycleLockMapper.selectOneByExample(example);

        Integer type = bicycleLock.getType();

        log.info("锁编号：" + lockNo + " 类型：" + type);

        // 回复锁登录信息

        String time = new SimpleDateFormat("yyMMddhhmmss").format(new Date());

        String content = "999" + "," + time;
        ctx.writeAndFlush(content);

    }


}
